﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.TeamFoundation.WorkItemTracking.Client;

using LeMigue.Scrum.Iterface.Service;
using LeMigue.Scrum.Iterface.Model;
using LeMigue.Scrum.TfsScrumV1.Utils;


namespace LeMigue.Scrum.TfsScrumV1.Service
{
    public class IterationService : AbstractTfsService, IIterationService
    {

        public IList<Iteration> List(Team team)
        {
            IList<Iteration> iterations = new List<Iteration>();

            foreach (Node no in TeamProjeto(team.Id).IterationRootNodes)
            {
                if (no.IsIterationNode)
                {
                    iterations.Add(criarIteration(no, team));

                }

            }

            return iterations;
        }

        public Iteration Load(int id)
        {
            throw new NotImplementedException();
        }

        public Iteration Save(Iteration item)
        {
            throw new NotImplementedException();
        }



        private Iteration criarIteration(Node no, Team team)
        {
            Iteration interacao = new Iteration(no.Id, no.Name);

            Sprint sprint = new SprintService().SprintByIteration(interacao);
            if (sprint != null)
            {
                sprint.Iteration = interacao;
                interacao.Sprint = sprint;
            }

            if (no.HasChildNodes)
            {
                foreach (Node subno in no.ChildNodes)
                {
                    if (subno.IsIterationNode)
                        interacao.AddChild(criarIteration(subno, team));
                }
            }

            return interacao;
        }

        /*internal Iteration Load(Microsoft.TeamFoundation.WorkItemTracking.Client.Project projeto, int idNode)
        {
            Node no = projeto.IterationRootNodes.GetNode(idNode);
            Iteration interacao = new Iteration(no.Id, no.Name);

            return interacao;
        }*/
    }
}
